package com.team1.system.blockchain.controller;

import com.team1.common.core.controller.BaseController;
import com.team1.system.blockchain.domain.Block;
import com.team1.system.blockchain.domain.BlockCache;
import com.team1.system.blockchain.domain.Transaction;
import com.team1.system.blockchain.service.BlockService;
import com.team1.system.blockchain.service.PowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class BlockController extends BaseController {

    @Autowired
    private BlockService blockService;
    @Autowired
    private PowService powService;

    @Autowired
    private BlockCache blockCache;

    /**
     * 查看当前节点区块链数据
     * @return
     */
    @ResponseBody
    public List<Block> upBlock(List<Transaction> transactions) {
        if (blockCache.getBlockChain().size()==0){
            //创建创世区块
            blockService.createGenesisBlock();
            powService.mine(transactions);
        }else {
            powService.mine(transactions);
        }
        return blockCache.getBlockChain();
    }
}

